package com.ruoyi.basedocument.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.basedocument.domain.BomAsscategory;
import com.ruoyi.basedocument.domain.BomCategory;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.util.BillUtil;
import com.ruoyi.util.importRunnable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basedocument.domain.BomCoefficient;
import com.ruoyi.basedocument.service.IBomCoefficientService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 系数Controller
 * 
 * @author ruoyi
 * @date 2024-09-06
 */
@RestController
@RequestMapping("/bomcostestimation/basedocument/coefficient")
public class BomCoefficientController extends BaseController
{
    @Autowired
    private IBomCoefficientService bomCoefficientService;
    @Autowired
    private importRunnable importRunnable;

    /**
     * 查询系数列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomCoefficient bomCoefficient)
    {
        startPage();
        List<BomCoefficient> list = bomCoefficientService.selectBomCoefficientList(bomCoefficient);
        return getDataTable(list);
    }

    /**
     * 导出系数列表
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:export')")
    @Log(title = "系数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomCoefficient bomCoefficient)
    {
        List<BomCoefficient> list = bomCoefficientService.selectBomCoefficientList(bomCoefficient);
        ExcelUtil<BomCoefficient> util = new ExcelUtil<BomCoefficient>(BomCoefficient.class);
        util.exportExcel(response, list, "系数数据");
    }

    /**
     * 获取系数详细信息
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:query')")
    @GetMapping(value = "/{fid}")
    public AjaxResult getInfo(@PathVariable("fid") String fid)
    {
        return success(bomCoefficientService.selectBomCoefficientByFid(fid));
    }

    /**
     * 新增系数
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:add')")
    @Log(title = "系数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomCoefficient bomCoefficient)
    {
        return toAjax(bomCoefficientService.insertBomCoefficient(bomCoefficient));
    }

    /**
     * 修改系数
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:edit')")
    @Log(title = "系数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomCoefficient bomCoefficient)
    {
        return toAjax(bomCoefficientService.updateBomCoefficient(bomCoefficient));
    }

    /**
     * 删除系数
     */
    @PreAuthorize("@ss.hasPermi('bomcostestimation/basedocument:coefficient:remove')")
    @Log(title = "系数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fids}")
    public AjaxResult remove(@PathVariable String[] fids)
    {
        return toAjax(bomCoefficientService.deleteBomCoefficientByFids(fids));
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void downTemplate(HttpServletResponse response) {
        ExcelUtil<BomCoefficient> util = new ExcelUtil<>(BomCoefficient.class);
        util.importTemplateExcel(response, "类别");
    }

    /**
     * 导入数据
     */
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestBody List< BomCoefficient > list) {
        importRunnable.removeParam();
        importRunnable.setBomCoefficientList(list);
        Thread thread = new Thread(importRunnable);
        thread.start();
        return toAjax(1);
//        for (int i = 0; i < list.size(); i++) {
//            BomCoefficient bomCoefficient = list.get(i);
//            bomCoefficientService.insertBomCoefficient(bomCoefficient);
//        }
    }
}
